/**
 * Project: eraclient
 * 
 * File Created at 2011-2-24
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.ymkyve.eraclient;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame {

    private JTextField     name_txt;
    private JPasswordField passwd;
    private JButton        confirm, concel,change;
    private String userName;
    private JLabel pass;
    public Login() {
        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel mainLabel = new JLabel("<html><font color=red size=5 >欢迎来到ERA客户端");
        mainLabel.setBounds(70, 10, 250, 30);
        container.add(mainLabel);
        JLabel name = new JLabel("<html><font color=black size=4>用户名:");
        name.setBounds(10, 50, 50, 20);
        container.add(name);
        name_txt = new JTextField();
        name_txt.setBounds(80, 50, 200, 20);
        container.add(name_txt);
        pass = new JLabel("<html><font color=black size=4> 密码:");
        pass.setBounds(10, 80, 50, 20);
        container.add(pass);
        passwd = new JPasswordField();
        passwd.setBounds(80, 80, 200, 20);
        container.add(passwd);

        confirm = new JButton("提交");
        confirm.setBounds(80, 120, 60, 30);
        container.add(confirm);
        confirm.addActionListener(new ActionEve());
        concel = new JButton("重置");
        concel.setBounds(160, 120, 60, 30);
        container.add(concel);
        concel.addActionListener(new ActionEve());

        this.setSize(300, 400);
        this.setLocation(getLocation());
        this.setVisible(true);
       // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void removeThis() {
        userName=name_txt.getText();
        this.remove(name_txt);
        this.remove(pass);
        this.remove(passwd);
        this.remove(confirm);
        this.remove(concel);
    }
    
    
    
    public void change() {
        this.removeThis();
        
        Container container = this.getContentPane();
        JLabel name = new JLabel("<html><font color=black size=4>用户名:"+userName);
        name.setBounds(10, 50, 250, 20);
        container.add(name);
        
        
        pass = new JLabel("<html><font color=black size=4> message:");
        pass.setBounds(10, 80, 50, 20);
        container.add(pass);
        
        change = new JButton("改变");
        change.setBounds(80, 120, 60, 30);
        container.add(change);
        change.addActionListener(new ActionEve());
        
        this.dispose();//隐藏
        setVisible(true);// 设置为可见
        setAlwaysOnTop(true);// 设置置顶
        // 设置窗口状态(在最小化状态弹出显示)
        setExtendedState(JFrame.NORMAL);
        
        
       
        //
        new SocketThread(this).start();
    }

    public void changeText(){
        this.pass.setText("changeText");
    }
    
    class SocketThread extends Thread {
        private Login login;
        public SocketThread(Login login){
            this.login = login;
        }
        
        /* (non-Javadoc)
         * @see java.lang.Thread#run()
         */
        public void run() {
            createSocket();
        }
        public void createSocket(){
            
            try {
                //创建ServerSocket,端口998
                //ServerSocket serverSocket = new ServerSocket(998);
                Socket socket = new Socket("127.0.0.1",998);
                StringBuffer sb = new StringBuffer();
                while(true){
                    TimeUnit.SECONDS.sleep(5);
                    //侦听并接受到此serverSocket的连接
                    
                    //输出流设置到标准输出流
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    //读取socket的输入流
                    InputStream  in = new DataInputStream(socket.getInputStream());
                    byte[] bRead = new byte[1024];
                    int iRead;
                    while ((iRead = in.read(bRead)) != -1) {
                        //写入输出流
                        out.write(bRead,0,iRead);
                    }
                    login.pass.setText(out.toString());
                    //pass.setText(out.toString());
                }
               // socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        
        }
    }
    
    
    
    class ActionEve implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == confirm) {

                if (name_txt.getText().length() == 0 || passwd.getPassword().length == 0) {
                    JOptionPane.showMessageDialog(Login.this,
                            "<html><font color=red size=5>必填内容不能为空");
                    passwd.setText(null);
                    
                } else {
                    change();
                }
            }
            if (e.getSource() == change) {

                changeText();
            }
        }

    }
}
//
//    public void readInfo(String s) throws IOException, ClassNotFoundException {
//        FileInputStream fis = new FileInputStream(s);
//        ObjectInputStream ois = new ObjectInputStream(fis);
//        while (true) {
//            try {
//                UserInfo user = (UserInfo) ois.readObject();
//                String string = "";
//                string += user.getName() + "\n";
//                string += user.getPassword() + "\n";
//                string += user.getBirthdate() + "\n";
//                string += user.getSex() + "\n";
//                string += user.getInterest() + "\n";
//                string += user.getOthers() + "\n";
//                JOptionPane.showMessageDialog(null, string);
//            } catch (EOFException e) {
//                return;
//            }
//        }
//    }

